//
//  HXDVisitorView.swift
//  weibo
//
//  Created by huxuedong on 15/12/5.
//  Copyright © 2015年 huxuedong. All rights reserved.
//

import UIKit
import SnapKit

class HXDVisitorView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //懒加载各个控件
    private lazy var iconView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_house")
        let imageView = UIImageView(image: image)
        return imageView
    }()
    
    private lazy var yuanView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(15)
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textAlignment = .Center
        label.numberOfLines = 0
        return label
    }()
    
    lazy var registerButton: UIButton = {
        let button = UIButton()
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return button
    }()
    
    lazy var loginButton: UIButton = {
        let button = UIButton()
        button.setTitle("登录", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        return button
    }()
    
    private lazy var coverView: UIView = {
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        let imageView = UIImageView(image: image)
        return imageView
    }()
    
    private func setupUI() {
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        //将控件添加到访客页面中
        addSubview(yuanView)
        addSubview(coverView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        //运用第三方框架添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        yuanView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(yuanView.snp_bottom).offset(15)
            make.centerX.equalTo(yuanView)
            make.width.equalTo(250)
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(15)
            make.size.equalTo(CGSizeMake(100, 35))
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(15)
            make.size.equalTo(CGSizeMake(100, 35))
        }
        coverView.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(loginButton)
        }
    }
    
    func setVisitorInfo(imageName imageName: String?, message: String?) {
        //imageName和message都为可选类型，当imageName和message都不为空时，name和info才能赋值成功，if let才会为真，反之，imageName和message有一个为空时，if let就为假
        if let name = imageName, info = message {
            yuanView.hidden = true
            iconView.image = UIImage(named: name)
            messageLabel.text = info
        } else {
            startAnimation()
        }
    }
    
    private func startAnimation() {
        //创建一个基础动画，动画的改变值为角度
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //角度改变的终点值
        animation.toValue = 2 * M_PI
        //动画执行时间
        animation.duration = 20
        //动画重复次数
        animation.repeatCount = MAXFLOAT
        //动画完成后不将动画移除
        animation.removedOnCompletion = false
        //为控件添加动画
        yuanView.layer.addAnimation(animation, forKey: nil)
    }
    
    private func demo() {
        //取消自动布局
        iconView.translatesAutoresizingMaskIntoConstraints = false
        //手动为控件布局
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        yuanView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: yuanView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: yuanView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: yuanView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 250))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: yuanView, attribute: .Bottom, multiplier: 1, constant: 15))
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Leading, relatedBy: .Equal, toItem: messageLabel, attribute: .Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 15))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Trailing, relatedBy: .Equal, toItem: messageLabel, attribute: .Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 15))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        coverView.translatesAutoresizingMaskIntoConstraints = false
        //VFL
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[coverView]-0-|", options: [], metrics: nil, views: ["coverView": coverView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[coverView]-(offset)-[registerButton]", options: [], metrics: ["offset" : -35], views: ["coverView": coverView,"registerButton": registerButton]))
    }

}
